<?php

use Drupal\shusy_store_order_line\Entity\ShusyStoreOrderLine;

/**
 * Implements hook_rules_action_info().
 */
function shusy_product_rules_action_info() {
  return [
    'shusy_product_rules_action_rollback_stock' => [
      'label' => t('库存回滚'),
      'parameter' => [
        'line' => [
          'type' => 'shusy_store_order_line',
          'label' => t('行项'),
        ],
      ],
      'group' => t('shusy_product'),
    ],
  ];
}

function shusy_product_rules_action_rollback_stock(ShusyStoreOrderLine $line) {
  if ($line instanceof ShusyStoreOrderLine && $line->status == 1 && $line->type == 'product') {
    $qty = $line->qty;
    $lineWrapper = $line->wrapper();
    $standardWrapper = $lineWrapper->standard;
    $standard = $standardWrapper->value();
    $standard->stockChange($qty);
  }
}
